package com.martian.service.impl;

import com.github.pagehelper.PageHelper;
import com.martian.common.util.ThreadLocalMap;
import com.martian.mapper.CommentMapper;
import com.martian.pojo.dto.AllCommentDto;
import com.martian.pojo.dto.CommentDetailDto;
import com.martian.pojo.dto.CommentDto;
import com.martian.pojo.form.CommentForm;
import com.martian.pojo.form.CommentLikeForm;
import com.martian.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author martian
 * @version V1.0.0
 * @Description
 * @date 2023-05-01 11:10
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    CommentMapper commentMapper;
    @Override
    public void publishComment(CommentForm commentForm) {
        Date curDate = new Date();
        Integer userId = (Integer) ThreadLocalMap.get("THREAD_LOCAL_KEY_LOGIN_USER");
        commentForm.setPublishDate(curDate);
        commentForm.setUserId(userId);
        System.out.println(commentForm.getToId());
        commentMapper.insertComment(commentForm);
    }

    @Override
    public List<CommentDetailDto> getComment(Integer movieId, Integer parentId) {
        List<CommentDetailDto> originComments = commentMapper.selectCommentForDetail(movieId,parentId);
        if (originComments.size() == 0){
            return null;
        }
        Set<Integer> disCommentIds = new HashSet<>();
        for (CommentDetailDto originComment : originComments) {
            disCommentIds.add(originComment.getCommentId());
        }
        List<CommentDetailDto> replys = commentMapper.selectInnerComment(disCommentIds);
        if (replys.size()!=0){
            Map<Integer,List<CommentDetailDto>> groupReplys = replys.stream().collect(Collectors.groupingBy(CommentDetailDto::getParentId));
            for (CommentDetailDto originComment : originComments) {
                List<CommentDetailDto> r = groupReplys.get(originComment.getCommentId());
                originComment.setReply(r);
                originComment.setReplyNum(r==null? 0 : r.size());
            }
        }
        return originComments;
    }

    @Override
    public AllCommentDto getAllComments(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<CommentDetailDto> commentDetailDtoList = commentMapper.selectAllComments();
        Integer num = commentMapper.selectNumComments();
        return new AllCommentDto(commentDetailDtoList,num);
    }

    @Override
    public void likeComment(CommentLikeForm commentLikeForm) {
        Integer userId = (Integer) ThreadLocalMap.get("THREAD_LOCAL_KEY_LOGIN_USER");
        Integer commentId = commentLikeForm.getCommentId();
        Integer num = commentMapper.selectCommentLike(userId,commentId);
        Date curDate = new Date();
        if (num == 0){
            commentMapper.likeComment(userId,commentId,1,curDate);
        }else{
            commentMapper.dislikeComment(userId,commentId);
        }
    }
}
